package com.kymatrix.mpo.plus.bpm.server.controller.task;

import cn.hutool.core.collection.CollUtil;
import com.kymatrix.mpo.plus.system.api.service.UserApi;
import com.kymatrix.mpo.plus.system.api.dto.UserDto;
import com.kymatrix.mpo.plus.bpm.server.model.entity.task.BpmProcessInstanceCopyEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.cc.BpmProcessInstanceCopyResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.instance.PageQueryBpmProcessInstanceCopyReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmProcessInstanceCopyService;
import com.kymatrix.mpo.plus.bpm.server.service.BpmProcessInstanceService;
import com.kymatrix.mpo.plus.bpm.server.service.BpmTaskService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import com.kymatrix.mpo.plus.common.springsecurity.SecurityUtils;
import com.kymatrix.mpo.plus.common.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertListByFlatMap;
import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertSet;


@Api(tags = "BPM 流程实例抄送相关请求处理类")
@RestController
@Validated
public class BpmProcessInstanceCopyController {

    @Resource
    private BpmProcessInstanceCopyService processInstanceCopyService;
    @Resource
    private BpmProcessInstanceService processInstanceService;
    @Resource
    private BpmTaskService taskService;

    @Resource
    private UserApi userApi;

    @GetMapping("/bpm/process-instance/copy/page")
    @ApiOperation(value = "获得抄送流程分页列表")
    public ResultResp<PageResp<BpmProcessInstanceCopyResp>> getProcessInstanceCopyPage(
            @Valid PageQueryBpmProcessInstanceCopyReq req) {
        PageResp<BpmProcessInstanceCopyEntity> pageResult = processInstanceCopyService.getProcessInstanceCopyPage(
                SecurityUtils.getLoginUserId(), req);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return ResultResp.ok(new PageResp<>());
        }

        // 拼接返回
        Map<String, String> taskNameMap = taskService.getTaskNameByTaskIds(
                convertSet(pageResult.getList(), BpmProcessInstanceCopyEntity::getTaskId));
        Map<String, HistoricProcessInstance> processInstanceMap = processInstanceService.getHistoricProcessInstanceMap(
                convertSet(pageResult.getList(), BpmProcessInstanceCopyEntity::getProcessInstanceId));
        Map<String, UserDto> userMap = userApi.getUserMap(convertListByFlatMap(pageResult.getList(),
                copy -> Stream.of(copy.getStartUserId(), copy.getCreator())));

        PageResp<BpmProcessInstanceCopyResp> resp = new PageResp<>();
        resp.setTotal(pageResult.getTotal());
        for (BpmProcessInstanceCopyEntity entity : pageResult.getList()) {
            BpmProcessInstanceCopyResp res = new BpmProcessInstanceCopyResp();
            res.setId(entity.getId());
            res.setStartUserId(entity.getStartUserId());
            if (userMap.containsKey(entity.getUserId())) {
                UserDto userDto = userMap.get(entity.getUserId());
                res.setStartUserName(userDto.getNickname());
                res.setCreatorName(userDto.getNickname());
            }
            res.setTaskId(entity.getTaskId());
            res.setTaskName(taskNameMap.get(entity.getTaskId()));
            
            res.setProcessInstanceId(entity.getProcessInstanceId());
            res.setProcessInstanceName(entity.getProcessInstanceName());
            if (processInstanceMap.containsKey(entity.getProcessInstanceId())) {
                HistoricProcessInstance historicProcessInstance = processInstanceMap.get(entity.getProcessInstanceId());
                res.setProcessInstanceStartTime(DateUtil.of(historicProcessInstance.getStartTime()));
            }
            
            res.setCreator(entity.getCreator());
            res.setCreateTime(entity.getCreateTime());
            resp.getList().add(res);
        }
        return ResultResp.ok(resp);
    }

}
